﻿using System;
using System.ComponentModel;
using System.Windows;

namespace GameLogic.Logic
{
    // TODO: WIP
    public class FollowElementBehavior : GameContainerBehavior<FrameworkElement>
    {
        #region Dependency properties
        [Category("FollowObject Properties")]
        public string TargetElement
        {
            get { return (string)GetValue(TargetElementProperty); }
            set { SetValue(TargetElementProperty, value); }
        }
        public static readonly DependencyProperty TargetElementProperty =
            DependencyProperty.Register(
                "TargetElement",
                typeof(string), 
                typeof(FollowElementBehavior), 
                new PropertyMetadata(null, null));        
        
        [Category("FollowObject Properties")]
        public PositionOptions FollowPosition
        {
            get { return (PositionOptions)this.GetValue(FollowPositionProperty); }
            set { this.SetValue(FollowPositionProperty, value); }
        }
        public static readonly DependencyProperty FollowPositionProperty =
          DependencyProperty.Register(
              "FollowPosition",
              typeof(PositionOptions),
              typeof(FollowElementBehavior),
              new PropertyMetadata(PositionOptions.XY, null));

        public enum PositionOptions
        {
            None,
            X,
            Y,
            XY
        };
        #endregion

        private Point center;
        private Point targetPos;
        private UIElement targetUIElement;

        protected override void OnGameContainerInitialized()
        {
            base.OnGameContainerInitialized();

            this.center = new Point(this.AssociatedObject.ActualWidth / 2, this.AssociatedObject.ActualHeight / 2);
            this.targetUIElement = this.GameContainer.GameControl.FindName(this.TargetElement) as UIElement;

            //this.GameContainer.GameLoop += this.OnGameContainer_GameLoop;
            //this.GameContainer.Reset += this.OnGameContainer_Reset;
        }

        private void OnGameContainer_GameLoop(object sender, EventArgs e)
        {
            if (this.targetUIElement != null && this.FollowPosition != PositionOptions.None)
            {
                Point position = this.AssociatedObject.TransformToVisual(targetUIElement).Transform(this.center); //DOENST WORK
                
                switch (this.FollowPosition)
                {
                    case PositionOptions.X:
                        targetPos.X = position.X - this.center.X;
                        break;

                    case PositionOptions.Y:
                        targetPos.Y = position.Y - this.center.Y;
                        break;

                    case PositionOptions.XY:
                        targetPos.X = position.X - this.center.X;
                        targetPos.Y = position.Y - this.center.Y;
                        break;
                }

                Rect newPos = new Rect(
                   targetPos.X,
                   targetPos.Y,
                   this.AssociatedObject.ActualWidth,
                   this.AssociatedObject.Height);
                this.AssociatedObject.Arrange(newPos);
            }
        }

        private void OnGameContainer_Reset(object sender, EventArgs e)
        {
            //this.GameContainer.GameLoop -= this.OnGameContainer_GameLoop;
            //this.GameContainer.Reset -= this.OnGameContainer_Reset;
        }
    }
}
